import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
  showSpinner: false
})


const routes = [{ 
  path: '/login',
 component: () => import('@/views/login/index.vue') , meta: { title: '登录' }
},
{
  path: '/',
  component: () => import('@/views/layout/index.vue'),
  redirect: '/home',
  children: [
    { path: '/home', component: () => import('@/views/home/index.vue'), meta: { title: '首页' } },
    { path: '/article', component: () => import('@/views/article/index.vue') ,meta: { title: '健康百科' }},
    { path: '/notify', component: () => import('@/views/notify/index.vue') ,meta: { title: '消息通知' }},
    { path: '/user', component: () => import('@/views/user/index.vue'),meta: { title: '个人中心' } }
  ]
},
{
  path: '/user/patient',
  component: () => import('@/views/user/PatientPage.vue'),
  meta: { title: '家庭档案' }
}

]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

import { useUserStore } from '@/stores'

// 访问权限控制
router.beforeEach((to, from) => {
  NProgress.start()
  
  // 用户仓库
  const store = useUserStore()
  // 不需要登录的页面，白名单
  const wihteList = ['/login']
  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
  console.log(store.user?.token)
  // 否则不做任何处理
})

router.afterEach((to) => {
  document.title = `${to.meta.title || ''}-优医问诊`
  NProgress.done()
})


export default router
